package com.alibaba.citrus.cr.mn.order.center.facade.repository.impl;

import com.alibaba.citrus.cr.common.exception.RepositoryException;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.OrderLineToOrderLineSDOConverter;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.OrderToOrderSDOConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.OrderQueryRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.domain.order.dto.LoadOrderListRequest;
import com.epoch.app.crb2btradecenter.domain.order.dto.LoadOrderRequest;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.crb2btradecenter.domain.order.service.OrderService;
import com.epoch.app.crb2btradecenter.domain.orderline.dto.LoadListResponse;
import com.epoch.app.crb2btradecenter.domain.orderline.dto.OrderLineLoadListRequest;
import com.epoch.app.crb2btradecenter.domain.orderline.service.OrderLineService;
import com.epoch.app.mnordercenter.model.dto.OrderLineSDO;
import com.epoch.app.mnordercenter.model.dto.OrderSDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @date 2023/5/26 17:02
 */
@Service("orderQueryRepositoryBiz")
public class OrderQueryRepositoryImpl implements OrderQueryRepository {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderLineService orderLineService;

    @Override
    public OrderSDO queryDetail(String id) {
        if(StringUtils.isBlank(id)){
            throw new RepositoryException("");
        }
        LoadOrderRequest loadOrderRequest = LoadOrderRequest.builder().build();
        loadOrderRequest.setId(id);
        Order order = orderService.loadOrder(loadOrderRequest);
        if (Objects.isNull(order)) {
            throw new RepositoryException("");
        }
        OrderSDO sdo = OrderToOrderSDOConvertor.convert(order);

        OrderLineLoadListRequest orderLineLoadListRequest = new OrderLineLoadListRequest();
        orderLineLoadListRequest.setMainOrderId(id);
        orderLineLoadListRequest.setStart(0);
        orderLineLoadListRequest.setLimit(999);
        LoadListResponse loadListResponse = orderLineService.loadList(orderLineLoadListRequest);
        List<OrderLineSDO> lineSDOList = Nullable.stream(loadListResponse.getResult()).map(x -> {
            OrderLineSDO lineSDO = OrderLineToOrderLineSDOConverter.convert(x);
            return lineSDO;
        }).collect(Collectors.toList());
        sdo.setOrderDetailList(lineSDOList);
        return sdo;
    }

    @Override
    public Result<List<OrderSDO>> queryByFrontOrderId(String frontOrderId) {
        LoadOrderListRequest loadOrderListRequest = new LoadOrderListRequest();
        loadOrderListRequest.setFrontOrderId(frontOrderId);
        Result<List<Order>> response = orderService.loadOrderList(loadOrderListRequest);
        List<OrderSDO> result = Nullable.stream(response.getResult()).map(OrderToOrderSDOConvertor::convert).collect(Collectors.toList());
        return Result.listSuccess(result,response.getTotal());
    }


}
